<?php
class Child_home extends MY_Controller{

	/**
	 * 获取孩子统计信息
	 * @return [type] [description]
	 */
	function get_statistics(){
		$data = $this->user_post_data;
    	if(!empty($data)){
    		$child_id = $data['childer_id'];

    		//孩子信息
    		$this->load->model('User_child_model');
    		$child_info = $this->User_child_model->get_info_by_id($child_id);

    		//统计习惯数量
    		$habit_total = $this->User_child_model->get_habit_count($child_id);
    		$grade_array = array(
    			'0'=>'学前班',
    			'1'=>'一年级',
    			'2'=>'二年级', 
    			'3'=>'三年级', 
    			'4'=>'四年级', 
    			'5'=>'五年级', 
    			'6'=>'六年级', 
    			'7'=>'七年级', 
    			'8'=>'八年级', 
    			'9'=>'九年级', 
    			'10'=>'高一', 
    			'11'=>'高二', 
    			'12'=>'高三', 
    			'13'=>'其他', 
    		);
    		
    		//返回数组
    		$info = array(
    			'child_id' => $data['childer_id'], 
    			'child_name' => $child_info['nick'],
    			'child_img' => $child_info['head_img_url'],
    			'gender' => $child_info['gender'],
    			'grade' => $grade_array[$child_info['grade']],
    			'stars_total' => $child_info['stars'],
    			'habit_total' => $habit_total,
    			'reward_total' => 0,
    			'learning_score' => "5.0"
    		);

			echo json_encode(array("code" => 1, "data" => $info));
    	}else{
            echo json_encode(array("code" => 404, "data" => 'not user'));
        }

	}

    function get_child_info(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $child_id = $data['childer_id'];

            //孩子信息
            $this->load->model('User_child_model');
            $child_info = $this->User_child_model->get_info_by_id($child_id);

           
            $grade_array = array(
                '0'=>'学前班',
                '1'=>'一年级',
                '2'=>'二年级', 
                '3'=>'三年级', 
                '4'=>'四年级', 
                '5'=>'五年级', 
                '6'=>'六年级', 
                '7'=>'七年级', 
                '8'=>'八年级', 
                '9'=>'九年级', 
                '10'=>'高一', 
                '11'=>'高二', 
                '12'=>'高三', 
                '13'=>'其他', 
            );
            
            //返回数组
            $info = array(
                'child_id' => $data['childer_id'], 
                'child_name' => $child_info['nick'],
                'child_img' => $child_info['head_img_url'],
                'gender' => $child_info['gender'],
                'grade' => $grade_array[$child_info['grade']]
            );

            echo json_encode(array("code" => 1, "data" => $info));
        }else{
            echo json_encode(array("code" => 404, "data" => 'not user'));
        }

    }

    /**
     * 获取所有习惯并获取前3条习惯记录
     * @return [type] [description]
     */
	function get_child_habit_report(){
		$data = $this->user_post_data;
    	if(!empty($data)){
    		$child_id = $data['childer_id'];

    		//组合data
    		$habit_data = array();

    		$this->load->model('User_child_model');
    		$habit_list = $this->User_child_model->get_habit_by_child($child_id);

    		foreach ($habit_list as $key => $habit) {
    			# code...
    			$habit_record = $this->User_child_model->get_habit_record($habit['id'],3);
    			$records = array();

    			foreach ($habit_record as $key => $record) {
    				# code...
    				$records[] = array(
    					'date' => date("Y-m-d", time()), 
    					'text' => $record['text'],
    					'image' => $record['image'],
    				);
    			}
    			$arrayName = array(
    				'habit_name' => $habit['name'],
    				'habit_start_end' => '2017.9.22-2017.11.30',
    				'habit_days' => '已坚持34天',
    				'records' => $records,
    			);

    			$habit_data[] = $arrayName;

    		}
    		$data_obj = array(
                'count' => count($habit_data),
                'list' => $habit_data,
             );
    		
    		echo json_encode(array("code" => 1, "data" => $data_obj));
    	}else{
            echo json_encode(array("code" => 404, "data" => 'not user'));
        }

	}

}